package m4;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 路由模式
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();//通信通道
        //定义direct类型交换机 direct_logs
        c.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);
        while (true){
            System.out.println("输入消息:");
            String msg = new Scanner(System.in).nextLine();
            System.out.println("请输入路由键:");
            String key = new Scanner(System.in).nextLine();
            //发送消息并携带路由键->两个消费者都匹配时都会收到消息,如果没有匹配的消费者则会丢失消息,交换机不存储数据
            c.basicPublish("direct_logs", key, null, msg.getBytes());

        }
    }
}
